package com.game.raceroom.player.contrl
{
	import com.util.DEF;
	import com.game.raceroom.player.Player;
	//侧滑控制
	public class SideClipContrl
	{

		private var actorContrl:Player;
  
		public function SideClipContrl(actorContrl:Player)
		{
			this.actorContrl=actorContrl;
		}
 
        public  var speed:Number=0;
        public var sideClipAngle:int=0;

	   //侧滑加速度	
       private var alpha:Number=1;
       //摩擦损失力
       private var frictLostRate:Number=0.8;
       private var F1,F2,F,temp,sideSpeed:Number;
	   private var  theta,oriAngle,angleF1;
       private var F1s2:Number;
  		private var F2s2:Number;
  		private var F1F2x2:Number;
    	private static var  i0:int=0;
  		private static var  i360:int=360;
  		private static var  i180:int=180;
  		private static var  i1:int=1;
  		private static var  i2:int=2;
  		private static var  i15:int=15;
  		private static var  i24:int=24;
  		private static var  i90:int=90;
  		private static var  i_1:int=-1;
  		private var number:Number;
       public function setSideClipData(eft:Number,spd:Number,angle:Number):void{
      	    hasSideClip=true;
            frictLostRate=actorContrl.carInfo.getSlipResistance()*eft;
            if(frictLostRate>=i1){
            	hasSideClip=false;
            	return;
            }
            //trace(frictLostRate+"   frictLostRate      "+spd +   "     "+angle   );
            // resistanceCft 侧滑时候的总阻力
            // eft 根据路面情况前进力的折算
            //汽车转向后 车辆的动力为
            if(spd<=i0)return;
            if(actorContrl._down){
            	angle+=i180;
            	angle%=i360;
            		actorContrl.down_speed=frictLostRate*spd;
            }else if(actorContrl._up){
            		actorContrl.up_speed=frictLostRate*spd;
            }
	        sideSpeed=(i1-frictLostRate)*spd;
	        alpha=actorContrl._accCft;
	         
            if(speed==i0){//没有其他作用力的情况下
	            //侧向惯性力（侧滑）
	            speed=sideSpeed;
	            sideClipAngle=angle;
	            
            }else{//有其他作用力
	            //侧向惯性力（侧滑）
	           // F=√（F1^2+F2^2+F1*F2*cosφ)
	           if(sideClipAngle>angle){
	      		    theta=sideClipAngle-angle;

	      		    if(theta>i180){
	      		    	theta=i360-theta;
                		F1=sideSpeed;
                		F2=speed;
                		oriAngle=sideClipAngle;
	      		    } else {
	          		    F1=speed;
                		F2=sideSpeed;
                		oriAngle=angle;
	      		    }
	           }else{
	           	    theta=angle-sideClipAngle;
	      		    if(theta>i180){
	      		    	theta=i360-theta;
	      		    	F1=speed;
                		F2=sideSpeed;
                		oriAngle=angle;
	      		    } else{
                		F1=sideSpeed;
                		F2=speed;
                		oriAngle=sideClipAngle;
	      		    }
	           }
 
      		    if(theta==i0){
      		    	speed=speed+sideSpeed;
      		    	return;
      		    }
      		    if(theta==i180){
      		    	if(speed>sideSpeed){
      		    		speed=speed-sideSpeed;
      		    		return;
      		    	}else{
      		    		speed=sideSpeed-speed;
      		    		sideClipAngle=angle;
      		    		return;
      		    	}
      		    }
      		    //余弦定理
    		    F1s2=F1*F1;
    		    F2s2=F2*F2;
    		    F1F2x2=F1*F2*i2;
    		    temp=F1F2x2*Math.cos(number=Math.PI-angleToRadiou(theta));
                F=Math.sqrt(number=F1s2+F2s2-temp);
                speed=F;
	            temp=(F*F+F2s2-F1s2)/(i2*F*F2);
	            if(temp>i1){
	              	temp=i1;
	            }
	            if(temp<i_1){
	            	temp=i_1;
	            }
	           // trace("b  "+temp );
	            temp=Math.acos(temp);
	            angleF1=radiouToAngle(temp);
//	            trace("angle  "+temp+"  " +angleF1+"  "+sideClipAngle+"  "+F+"  "+F1+"  "+F2+"  "+theta);
	            sideClipAngle=oriAngle+angleF1;
	            sideClipAngle=sideClipAngle%i360;
 
            }
       }
       var ret:Number;
      private  function angleToRadiou(angle:int):Number{
       	   ret=angle/i360*Math.PI*i2;
       	   return ret;
       }
       private  function radiouToAngle(radiou:Number):int{
       	   ret=radiou/(Math.PI*i2)*i360;
       	   return ret;
       }      
       public var tx,ty:Number;
       private var avgSpd:Number=0;
       public var hasSideClip:Boolean=false;
       public function nextSide(time:Number):Boolean{
       	    avgSpd=sideClip(time);
       	    if(avgSpd<=i0)hasSideClip=false;
            //if(avgLost<=0)actorContrl.in_side_clip=false;
            if(actorContrl._down){
                temp=Math.floor(number=(actorContrl._angle+i180)/i15)%i24;
            }else{
            	temp=Math.floor(number=actorContrl._angle/i15)%i24;
            }
            //if(avgLost<=0)actorContrl.in_side_clip=false;
 
			actorContrl._vx=DEF.sinTable[temp]*(actorContrl._speed);
			actorContrl._vy=DEF.cosTable[temp]*(actorContrl._speed)*i_1;
			tx=Math.sin(number=angleToRadiou(sideClipAngle))*avgSpd;
			ty=Math.cos(number=angleToRadiou(sideClipAngle))*avgSpd*i_1;
			actorContrl._vx=actorContrl._vx+tx;
        	actorContrl._vy=actorContrl._vy+ty; 
            return true;
         
       }
       
	    private var startTime,nowTime:Number=0;
	   private  var t0,t1,t2,V=0,V0,V1,a,startV,endV:Number;
		public function sideClip(time:Number):Number{
		    V=speed;
		    V1=i0;
		    if(V>V1){
		    	a= alpha;
		    	startTime=V/a;
		    	nowTime=startTime-time;
		    	t0=	startTime;
		    	t2= nowTime;
				startV=t0*a;
				endV=t2*a;
				if(endV<i0){
					t1=i0;
					endV=i0;
					V0=(startV+endV)/i2;
					V=V0*(t0-t1)/(t0-t2);

				}else{
					V=(startV+endV)/i2;
				}
			    speed=endV;
		    }else{
	    	    speed=i0;
		    	return V1;
		    }
		    return V;		
		}
		private var spd,ang:Number;
        public function addCrash(vx:Number,vy:Number):void{
        	 
        	spd=Math.sqrt(number=vx*vx+vy*vy);	
        	ang=radiouToAngle(number=Math.atan2(vy*i_1,vx));
            if(ang<=i0){
             	  ang=i90-ang;
             }else{
             	  ang=i90-ang;
             	  ang=(ang+i360)%i360;
             }
            hasSideClip=true;
            alpha=actorContrl._accCft;
            actorContrl._speed=i0;
            actorContrl.up_speed=i0;
            actorContrl.down_speed=i0;
            this.sideClipAngle=ang;
            this.speed=spd;
           // actorContrl.resetAction();
            //setSideClipData(actorContrl.controlCft,spd,ang,false);

        }

//        public function addCrash(vx:Number,vy:Number,eft:Number,time:Number):void{
//        	
//        	var spd=Math.sqrt(vx*vx+vy*vy);	
//            var ang=this.radiouToAngle( Math.atan2(vy*-1,vx));
//           // ang=90-ang;
//           if(ang<=0){
//           	  ang=90-ang;
//           }else{
//           	  ang=90-ang;
//           	  ang=(ang+360)%360;
//           }
//            if(actorContrl._down){
//            	ang-=180;
//            }
//            //actorContrl.controlCft=eft;
//             setSideClipData(eft,spd,ang);
////             trace(" crash    "+ang+"  "+radiouToAngle( Math.atan2(vy*-1,vx))+"  "+vx+"  "+vy+"  name--"
////              +this.actorContrl.userInfo.carName
////             );
//        }
	}
}